/** This Snippets is used for run something (a Chunk include another snippet or file) automatically
*   for example if you set up run a php file every 24hrs to update a rss feed
*   this snippet will run the file when the first visitor visit the site. every 24 hrs
*   $ifChunk   set up the chunk name you would like to run after a certain duration like 24hrs
*   $elseChunk set up the chunk name you would like to run if the time is not over 24hrs
*   $hrs  setup the duration based on hrs
*   $recordname this is the name of the  trigger you want to use
*   you have to preset up before using,  just go to PHPMyAdmin and create a new record
*   set the autorunname to the one you want, You can set up as many as you want,
*   each of them can trigger one event.
*   Write By Bright @ 20080808 
*   Email:      modx@brightqi.com
**/



$output = '';
$ifChunk = (isset($ifChunk))? $ifChunk : 'UPDATECURRENCY';
$elseChunk = (isset($elseChunk))? $elseChunk : '';
$recordname = (isset($recordname)) ? $recordname : 'currency';
$hrs = (isset($hrs)) ? $hrs : 24;
$secs = 3600*$hrs;



$rs = $modx->db->select('tstamp', 'autorun_checkpoint', 'autorunname = "' .$recordname. '"');

	if( $modx->db->getRecordCount( $rs ) >= 1 ) {
		while( $r = $modx->db->getRow( $rs ) ) {
			$t = $r['tstamp'];
		}
	}
	else {
			$t = 3;
	}


$time = time();
$duration = $time - $t;

if ($duration > $secs) {
	$modx->db->update("tstamp = $time", "autorun_checkpoint", "autorunname = '$recordname'"); 

	$output = $modx->getChunk($ifChunk);
} else {
	$output = $modx->getChunk($elseChunk);
}

return $output;